﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace community
{
    public partial class hospitaldictionary : Form
    {
        string strConn = SQL.conStr;
        string strSQL = "select hospital_name_chr,address_vchr,phone_number_chr,postcode_vchr,comment_vchr from t_communityname";

        SqlConnection con;
        SqlCommand cmd;

        public hospitaldictionary()
        {
            InitializeComponent();
        }

        private void hospitaldictionary_Load(object sender, EventArgs e)
        {//界面加载时，自动读取数据库中的数据
            con = new SqlConnection(strConn);
            if (con.State==ConnectionState .Closed  )
                con.Open();

            cmd = new SqlCommand(strSQL , con );

            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                hospitalName.Text = rdr["hospital_name_chr"].ToString();
                address.Text = rdr["address_vchr"].ToString ();
                phoneNuber.Text = rdr["phone_number_chr"].ToString();
                post.Text = rdr["postcode_vchr"].ToString();
                remark.Text = rdr["comment_vchr"].ToString();
            }

            rdr.Close();
            con.Close();
            

        }

        private void button1_Click(object sender, EventArgs e)
        {//对更新的数据，提交到数据库中。
            
            //string s=hospitalName.Text.Trim ();

            if(hospitalName.Text.Trim ()=="")
                {
                    MessageBox.Show ("医院名称不能为空！");
                    return;
                }
            //更新语句；
            strSQL = "update t_communityname set hospital_name_chr ='"+hospitalName.Text.Trim ()+
                    "',address_vchr='"+address.Text .Trim ()+
                    "',phone_number_chr='"+phoneNuber .Text .Trim ()+
                    "',postcode_vchr='"+post .Text .Trim ()+
                    "',comment_vchr='"+remark.Text.Trim ()+"'";

            con = new SqlConnection(strConn);
            if (con.State == ConnectionState.Closed)
                con.Open();
            cmd = new SqlCommand(strSQL, con);

            int i;
            try
            {
                 i = cmd.ExecuteNonQuery();
                if (i == 1)
                    MessageBox.Show("恭喜！保存成功！");
                else
                {
                    
                    MessageBox.Show("保存失败！");
                }
            }catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }


            con.Close();


        }
    }
}
